<?php
require_once("Xiachufang/entity/Recipe.php");
class RecipeLogic {
	/**
	 * 分页获取菜谱
	 *
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 * @return PageResult
	 */
	static function pagedGetRecipes($page, $pageSize) {
		$table = RecipeEntity::getTable();
		$sql = "select recipe_id from ".$table." order by recipe_id desc";
		$db = DBMgr::getSlaver($table);
		return $db->fullPagedQuery($sql, $page, $pageSize);
	}

	/**
	 * 获取最热的菜谱
	 *
	 * @param unknown_type $num
	 * @return unknown
	 */
	static function getHotRecipeIds($num=3) {
		$table = RecipeEntity::getTable();
		$sql = "select recipe_id from ".$table." order by view_num desc limit ".$num;
		$db = DBMgr::getSlaver($table);
		return $db->query($sql)->asArray1();
	}
	
	/**
	 * 获取最新的菜谱
	 *
	 * @param unknown_type $num
	 * @return unknown
	 */
	static function getNewRecipeIds($num=3) {
		$table = RecipeEntity::getTable();
		$sql = "select recipe_id from ".$table." order by recipe_id desc limit ".$num;
		$db = DBMgr::getSlaver($table);
		return $db->query($sql)->asArray1();
	}
	
	/**
	 * 寻找与指定名称类似的菜谱
	 *
	 * @param unknown_type $name
	 * @return unknown 返回包含 same（相同）、like（相似）两项的数组。
	 */
	static function findRecipe($name) {
		$table = RecipeEntity::getTable();
		$sql = "select recipe_id recipeId,name from ".$table." where name like ?";
		$db = DBMgr::getSlaver($table);
		$rs = $db->query($sql, array('%'.$name.'%'));
		$ret = array("same"=>NULL,"like"=>array());
		while($rs->hasNext()) {
			$row = $rs->next();
			if ($row["name"] == $name) {
				$ret["same"] = $row;
			} else {
				$ret["like"][] = $row;
			}
		}
		return $ret;
	}
	
	/**
	 * 查询菜谱
	 *
	 * @param unknown_type $keyword
	 * @return unknown
	 */
	static function search($keyword) {
		$table = RecipeEntity::getTable();
		$sql = "select recipe_id from ".$table." where name like ? limit 50";
		$db = DBMgr::getSlaver($table);
		$rs = $db->query($sql, array('%'.$keyword.'%'));
		return $rs->asArray1();
	}

	/**
	 * 获取菜谱名称的验证码
	 *
	 * @param unknown_type $name
	 * @return unknown
	 */
	static function getRecipeNameCheckCode($name) {
		return md5("__REC".$name."IPE_%");
	}
}
?>